package cn.tedu.text;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class TextDemo {
    public static void main(String[] args) {
        //1.声明流对象并且赋值为null
        FileReader reader = null;
        FileWriter writer = null;
        try {
            //流对象进行初始化
            //指定读取文件的位置
            reader = new FileReader("D:\\c.txt");
            writer = new FileWriter("D:\\javase\\c.txt");
            //读数据
            //自建缓冲区
            char[] cs = new char[1024 * 1024];
            int len = -1;
            while ((len = reader.read(cs)) != -1) {
                //读取数据已经存到数组中，从数组中往外写出
                //边读边写
                writer.write(cs, 0, len);
            }
            //4.手动冲刷
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //2.保证流对象初始化成功才能关流
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    //3.回收流对象
                    reader = null;
                }

                if (writer != null) {
                    try {
                        writer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        //3.回收流对象
                        writer = null;
                    }
                }
            }
        }
    }
}
